/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ranch.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.ranch.api.service.AnimalSearchParameters;
import org.ranch.dao.AnimalDAO;
import org.ranch.model.Animal;
import org.springframework.stereotype.Repository;

/**
 * Implements the {@link AnimalDAO} interface to provide CRUD operations for the
 * {@link Animal} entities using hibernate as the ORM strategy
 */
@Repository("animalDAO")
public class HibernateAnimalDAOImpl extends BaseDAOImpl<Animal> implements AnimalDAO {

    public Animal searchWithParams(String query, Integer pageNo, int pageSize) {
	/*
	 * Criteria criteria = buildCriteria(query);
	 * criteria.setFirstResult((pageNo - 1) * pageSize);
	 * criteria.setMaxResults(pageSize);
	 */
	/*
	 * List<Animal> a = (List<Animal>)
	 * getSession().createCriteria(Animal.class)
	 * .add(Restrictions.like("ear_tag", query,
	 * MatchMode.START).ignoreCase()).list();
	 */
	return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Animal> searchAnimalByTagNo(String query, String kraalIds) {
	return (List<Animal>) getSession().createCriteria(Animal.class)
		.add(Restrictions.like("earTag", query, MatchMode.START))
		.add(Restrictions.eq("kraal.id", kraalIds)).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Animal> searchWithParams(AnimalSearchParameters params, Integer pageNo,
	    int pageSize) {
	Criteria criteria = buildCriteria(params);
	criteria.setFirstResult((pageNo - 1) * pageSize);
	criteria.setMaxResults(pageSize);
	return criteria.list();
    }

    /**
     * @param params
     * @return
     */
    private Criteria buildCriteria(AnimalSearchParameters params) {
	Criteria animalCriteria = super.getSession().createCriteria(Animal.class);
	if(params.getType() != null){
	    animalCriteria.add(Restrictions.eq("type.id", params.getType().getId()));
	}
	
	if(params.getHealthStatus() != null){
	    animalCriteria.add(Restrictions.eq("healthStatus.id", params.getHealthStatus().getId()));
	}
	
	if(params.getColor() != null){
	    animalCriteria.add(Restrictions.eq("colour.id", params.getColor().getId()));
	}
	
	if(params.getBreed() != null){
	    animalCriteria.add(Restrictions.eq("breed.id", params.getBreed().getId()));
	}
	
	if(params.getStatus() != null){
	    animalCriteria.add(Restrictions.eq("status.id", params.getStatus().getId()));
	}
	
	if(params.getKraal() != null){
	    animalCriteria.add(Restrictions.eq("kraal.id", params.getKraal().getId()));
	}
	
	if(params.getWeight() > 0){
	    animalCriteria.add(Restrictions.eq("weight", params.getWeight()));
	}
	
	return animalCriteria;
    }
    

    @Override
    public long numberOfAnimalWithSearchParams(AnimalSearchParameters params) {
	Criteria criteria = buildCriteria(params);
	criteria.setProjection(Projections.rowCount()); 
	return ((Long) criteria.list().get(0)).longValue();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Animal> searchWithParams(AnimalSearchParameters params) {
	Criteria criteria = buildCriteria(params);
	return criteria.list();
    }

}
